package com.neuedu.maven;

import java.sql.*;

public class Test {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //1.注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");

        //2.获取链接
        String url = "jdbc:mysql://localhost:3306/wuye?characterEncoding=utf8&useSSl=false&serverTimezone=UTC";
        Connection conn = DriverManager.getConnection(url, "root", "123456");

        //3.获取执行对象
        PreparedStatement ps = conn.prepareStatement("select * from user");

        //4.执行
        ResultSet rs = ps.executeQuery();

        //5.查询结果遍历
        while (rs.next()) {
            String username = rs.getString("username");
            System.out.println(username);
        }

        //6.关资源
        if (rs != null) {
            rs.close();
        }
        if (ps != null) {
            ps.close();
        }
        if (conn != null) {
            conn.close();
        }

    }
}
